<?php
class ModelReportCustomer extends Model {
	public function getOrders($filter) {
		$sql = "SELECT tmp.customer_id, tmp.customer, tmp.email, tmp.customer_group, tmp.status, COUNT(tmp.order_id) AS orders, SUM(tmp.products) AS products, SUM(tmp.total) AS total FROM (SELECT o.order_id, c.customer_id, CONCAT(o.firstname, ' ', o.lastname) AS customer, o.email, cgd.name AS customer_group, c.status, (SELECT SUM(op.quantity) FROM `@@order_product` op WHERE op.order_id = o.order_id GROUP BY op.order_id) AS products, o.total FROM `@@order` o LEFT JOIN `@@customer` c ON (o.customer_id = c.customer_id) LEFT JOIN  @@customer_group_description cgd ON (c.customer_group_id = cgd.customer_group_id) WHERE o.customer_id > 0 AND cgd.language_id = '" . (int)C('config_language_id') . "'";

		if ($filter->get('filter_order_status_id') !== null) {
			$sql .= " AND o.order_status_id = '" . (int)$filter->get('filter_order_status_id') . "'";
		} else {
			$sql .= " AND o.order_status_id > '0'";
		}

		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(o.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}

		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(o.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}

		$sql .= ") tmp GROUP BY tmp.customer_id ORDER BY total DESC";

		$sql .= $filter->limit();

			$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalOrders($filter) {
		$sql = "SELECT COUNT(DISTINCT o.customer_id) AS total FROM `@@order` o WHERE o.customer_id > '0'";
		
		if ($filter->get('filter_order_status_id') !== null) {
			$sql .= " AND o.order_status_id = '" . (int)$filter->get('filter_order_status_id') . "'";
		} else {
			$sql .= " AND o.order_status_id > '0'";
		}
	
		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(o.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}
	
		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(o.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}

		$query = $this->db->query($sql);

		return $query->row['total'];
	}

	public function getRewardPoints($filter) {
		$sql = "SELECT cr.customer_id, CONCAT(c.firstname, ' ', c.lastname) AS customer, c.email, cgd.name AS customer_group, c.status, SUM(cr.points) AS points, COUNT(o.order_id) AS orders, SUM(o.total) AS total FROM  @@customer_reward cr LEFT JOIN `@@customer` c ON (cr.customer_id = c.customer_id) LEFT JOIN  @@customer_group_description cgd ON (c.customer_group_id = cgd.customer_group_id) LEFT JOIN `@@order` o ON (cr.order_id = o.order_id) WHERE cgd.language_id = '" . (int)C('config_language_id') . "'";

		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(cr.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}
		
		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(cr.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}
		
		$sql .= " GROUP BY cr.customer_id ORDER BY points DESC";
		$sql .= $filter->limit();
		
		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalRewardPoints($filter) {
		$sql = "SELECT COUNT(DISTINCT customer_id) AS total FROM `@@customer_reward` WHERE 1";

		if ($filter->get('filter_date_start')) {
			$sql .= " AND DATE(cr.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}
			
		if ($filter->get('filter_date_end')) {
			$sql .= " AND DATE(cr.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}
		
		$query = $this->db->query($sql);

		return $query->row['total'];
	}

	public function getCredit($filter) {
		$sql = "SELECT ct.customer_id, CONCAT(c.firstname, ' ', c.lastname) AS customer, c.email, cgd.name AS customer_group, c.status, SUM(ct.amount) AS total FROM  @@customer_transaction ct LEFT JOIN `@@customer` c ON (ct.customer_id = c.customer_id) LEFT JOIN  @@customer_group_description cgd ON (c.customer_group_id = cgd.customer_group_id) WHERE cgd.language_id = '" . (int)C('config_language_id') . "'";

		if ($filter->get('filter_date_start')) {
			$sql .= "DATE(ct.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}

		if ($filter->get('filter_date_end')) {
			$sql .= "DATE(ct.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}
		
		$sql .= " GROUP BY ct.customer_id ORDER BY total DESC";

		$sql .= $filter->limit();
		
		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalCredit($filter) {
		$sql = "SELECT COUNT(DISTINCT customer_id) AS total FROM `@@customer_transaction` WHERE 1";

		if ($filter->get('filter_date_start')) {
			$sql .= "DATE(ct.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
		}

		if ($filter->get('filter_date_end')) {
			$sql .= "DATE(ct.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
		}

		$query = $this->db->query($sql);

		return $query->row['total'];
	}
}
?>